var monthId;
var fromData;
var toData;
Ext.define('AM.controller.Contr', {
    extend: 'Ext.app.Controller',
    views: [
            'grid.List',
            'grid.GridDetails',
            'grid.BudgetGrid',

            'window.AddRow',
            'window.AddTree',
            'window.EditTree',
            'window.Login',
            'window.ChangeWindow',
            'window.Tools',

            'tree.Trees',
            
            'combo.ComboCild',
            'combo.ComboSubpro',
            'combo.ComboPro',
            'combo.ComboTip',
            'combo.ComboDetail',
            'combo.ComboLuni',

            'panel.Meniu',
            'panel.StartPanel',
            'panel.DetailPanel',
            'panel.DetailPanel1',
            'panel.GeneralPanel',
            'panel.Test3Panel',

            'panel.GraficPanel',
            'panel.DetailGraphicPanel',

            'data.Calendars',
            'data.MemoryCalendarStore',
            'data.CalendarMappings',
            'data.CalendarModel',
            'data.EventMappings',
            'data.EventModel',
            'data.Events',
            'data.MemoryEventStore',
            
            'util.WeekEventRenderer',
            'util.Date',
            'Day',
            'Week',
            'Month',
            'form.EventWindow',
            'form.EventDetails',
            'form.field.ReminderCombo',
            'form.field.DateRange',
            'form.field.CalendarCombo',
            
            'AbstractCalendar',
            'DayHeader',
            'DayBody',
            'MonthDayDetail',
            
            'dd.DayDragZone',
            'dd.DayDropZone',
            'dd.StatusProxy',
            'dd.DropZone',
            'dd.DragZone',
            
            'template.BoxLayout',
            'template.DayBody',
            'template.DayHeader',
            'template.Month',
            ],
    stores:['StoreGrid',
            'StoreBudgetGrid',
            'StoreTree',
            'Storetest',
            'StoreInsert',
            "StoreComboSubpro",
            'StoreComboCild',
            'StoreComboPro',
            'StoreComboTip',
            'StoreComboDetail',
            'StoreComboLuni',
            'StoreGrafic',
            'StoreDetailGraphic',
            'StoreDetail',
            'StoreDetailSum',
            'StoreColor'
            ],
    models: ['ModelGrid','TreeModel',"ModelCombo",'ModelGrafic','ModelInsert','ModelCalendar','ModelBudgetGrid'],
    refs: [{
		ref: 'addrow',
		selector: 'addrow'
	}],
    init: function() {
        this.control({
            'viewport > userlist': {
                //itemdblclick: this.editUser
            },
            'userlist button[action=add]': {
                click: this.showaddNew
            },
            'userlist button[action=delete]': {
                click: this.deleteRow
            },
            'addrow button[action=save]':{
            	click: this.addNew
            },
            'addrow button[action=update]':{
            	click:this.updateUser
            },
            'edittree button[action=editTree]':{
                click: this.EditTree
            },
            'subpro':{
            	select: this.loadStoreCild,
            	keypress: this.editSubpro
            },
            'detail':{
            	select: this.loadStoreDetail,
                expand: this.removeSelected
            },
            'luni':{
            	select: this.loadLuni,
                expand: this.removeSelected
            },
            'sum_init':{
                blur: this.test,
            },
            'detailpanel1 datefield[action=to]':{
            	select: this.loadTo,
            },
	        'detailpanel1 datefield[action=from]':{
	        	select: this.loadTo,
	        },
            'treepane':{
            	itemclick: this.clickTree
            },
            'treepane button[action=add]':{
            	click: this.addTree
            },
            'userlist': {
                itemdblclick: this.editUser
            },
            'gridDetails': {
                itemclick: this.loadDetails
            },
            'treepane button[action=i]': {
                itemclick: this.test
            },
            'addtree button[action=savee]':{
            	click:this.addTree
            },
            'login button[action=loginn]':{
            	click: this.Login
            },
            
        });
    },
    test: function(obj, s){
    	   	console.log('este');
    	   	Ext.Msg.alert('Status', 'Suma Initiala');
    },
    Login: function(obj, e) {
    	console.log('Login...');
    	//console.log(selecttree);
    },
    EditTree: function (obj,e) {
        console.log('Edit Tree insert...');
        var win = obj.up('window');
        text = win.down('#edit').getValue();
//    	tip = win.down('tip').getValue();
        if(selectTree[0].data.iconCls == 'task'){
//    		console.log('task');
            Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
                params: {
                    storeTip: 'editChild',
                    acction: 'editChild',
                    id: selectTree[0].data.id,
                    task: text,
                    UserID: UserID
                },
                success: function(response){
//                    var obj = Ext.decode(response.responseText);
//                    var responseData = obj.rows[0];
                    var node = Ext.getCmp('treepane').store.getNodeById(selectTree[0].data.id);
                    node.set('task', text);
//                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
                }
            });
        }
        else if(selectTree[0].data.iconCls=='task-folder'){
//    		console.log('task-folder');
            Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
                params: {
                    storeTip: 'editSubPro',
                    acction: 'editsubpro',
                    id: selectTree[0].data.idd,
                    task: text,
                    UserID: UserID
                },
                success: function(response){
//                    var obj = Ext.decode(response.responseText);
//                    var responseData = obj.rows[0];
                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
//                    var node = Ext.getCmp('treepane').store.getNodeById(selectTree[0].data.idd);
//                    node.set('task', text);
                }
            });
        }
        else if(selectTree[0].data.iconCls=='task-folder-root'){
//    		console.log('task-folder-root');
            Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
                params: {
                    storeTip: 'editPro',
                    acction: 'editpro',
                    id: selectTree[0].data.iddd,
                    task: text,
                    UserID: UserID
                },
                success: function(response){
//                    var obj = Ext.decode(response.responseText);
//                    var responseData = obj.rows[0];
                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
//                    var node = Ext.getCmp('treepane').store.getNodeById(selectTree[0].data.iddd);
//                    node.set('task', text);
                }
            });
        }
        win.close();
    },
    editPro: function(obj, e) {
    	console.log('Sel...Pro');
    },
    editSubpro: function(obj, e) {
    	var win = obj.up('window');
   	 	win.down('tip').show();
    },
    editCild: function(obj, e) {
    	console.log('Sel...Cild');
    },
    clickTree: function(obj, e) {
        var me = this;
        var treeData = e.data;
    	var filter = Ext.getCmp('filterButton').getText();
    	if(e.data.iconCls=='task'){
        	//console.log(e.data.id);
        	//console.log(e.parentNode.data.task);
        	//console.log(e.parentNode.parentNode.data.task);
        	//console.log(Ext.getCmp('userlist'));
            if(Ext.getCmp('generalpanel').getActiveTab().getId() == 'detailGraphic'){
                Ext.getStore('StoreDetailGraphic').load({params: {storeTip: 'detailGraphic', acction: 'child', period: (filter=='Years' ? 'year' : 'month'), child: treeData.id, UserID: UserID},
                    callback : function(store, data) {
                        me.removeChart();
                        if(filter == 'Months'){
                            me.addSeries('Jan - ' + treeData.task, 'Jan');
                            me.addSeries('Feb - ' + treeData.task, 'Feb');
                            me.addSeries('Mar - ' + treeData.task, 'Mar');
                            me.addSeries('Apr - ' + treeData.task, 'Apr');
                            me.addSeries('May - ' + treeData.task, 'May');
                            me.addSeries('Jun - ' + treeData.task, 'Jun');
                            me.addSeries('Jul - ' + treeData.task, 'Jul');
                            me.addSeries('Aug - ' + treeData.task, 'Aug');
                            me.addSeries('Sep - ' + treeData.task, 'Sep');
                            me.addSeries('Oct - ' + treeData.task, 'Oct');
                            me.addSeries('Nov - ' + treeData.task, 'Nov');
                            me.addSeries('Dec - ' + treeData.task, 'Dec');
                        } else {
                            me.addSeries('2012 - ' + treeData.task, '2012');
                            me.addSeries('2013 - ' + treeData.task, '2013');
                            me.addSeries('2014 - ' + treeData.task, '2014');
                            me.addSeries('2015 - ' + treeData.task, '2015');
                            me.addSeries('2016 - ' + treeData.task, '2016');
                            me.addSeries('2017 - ' + treeData.task, '2017');
                            me.addSeries('2018 - ' + treeData.task, '2018');
                            me.addSeries('2019 - ' + treeData.task, '2019');
                            me.addSeries('2020 - ' + treeData.task, '2020');
                            me.addSeries('2021 - ' + treeData.task, '2021');
                            me.addSeries('2022 - ' + treeData.task, '2022');
                            me.addSeries('2023 - ' + treeData.task, '2023');
                        }
                        var chart = Ext.getCmp('chartDetail');
                        chart.redraw();
                    }
                });
            } else {
        	    Ext.getCmp('userlist').getStore().load({
        		    params: {storeTip: 'grid', acction: 'cild', period: 'year', cild: e.data.id, UserID: UserID}
        	    });
        	}
    	}
    	else if(e.data.iconCls=='task-folder'){
    		//console.log(e.data.task);
        	//console.log(e.data.idd);
        	//console.log(e.parentNode.data.task);
        	if(Ext.getCmp('generalpanel').getActiveTab().getId() == 'detailGraphic'){
                Ext.getStore('StoreDetailGraphic').load({params: {storeTip: 'detailGraphic', acction: 'subPro', period: (filter=='Years' ? 'year' : 'month'), subPro: treeData.idd, UserID: UserID},
                    callback : function() {
                        me.removeChart();
                        if(filter == 'Months'){
                            me.addSeries('Jan - ' + treeData.task, 'Jan');
                            me.addSeries('Feb - ' + treeData.task, 'Feb');
                            me.addSeries('Mar - ' + treeData.task, 'Mar');
                            me.addSeries('Apr - ' + treeData.task, 'Apr');
                            me.addSeries('May - ' + treeData.task, 'May');
                            me.addSeries('Jun - ' + treeData.task, 'Jun');
                            me.addSeries('Jul - ' + treeData.task, 'Jul');
                            me.addSeries('Aug - ' + treeData.task, 'Aug');
                            me.addSeries('Sep - ' + treeData.task, 'Sep');
                            me.addSeries('Oct - ' + treeData.task, 'Oct');
                            me.addSeries('Nov - ' + treeData.task, 'Nov');
                            me.addSeries('Dec - ' + treeData.task, 'Dec');
                        } else {
                            me.addSeries('2012 - ' + treeData.task, '2012');
                            me.addSeries('2013 - ' + treeData.task, '2013');
                            me.addSeries('2014 - ' + treeData.task, '2014');
                            me.addSeries('2015 - ' + treeData.task, '2015');
                            me.addSeries('2016 - ' + treeData.task, '2016');
                            me.addSeries('2017 - ' + treeData.task, '2017');
                            me.addSeries('2018 - ' + treeData.task, '2018');
                            me.addSeries('2019 - ' + treeData.task, '2019');
                            me.addSeries('2020 - ' + treeData.task, '2020');
                            me.addSeries('2021 - ' + treeData.task, '2021');
                            me.addSeries('2022 - ' + treeData.task, '2022');
                            me.addSeries('2023 - ' + treeData.task, '2023');
                        }
                        var chart = Ext.getCmp('chartDetail');
                        chart.redraw();
                    }
                });
            } else {
        	    Ext.getCmp('userlist').getStore().load({
        		    params: {storeTip: 'grid', acction: 'categ', period: 'year', subpro: e.data.idd, UserID: UserID}
        	    });
        	}
    	}
    	else if(e.data.iconCls=='task-folder-root'){
        	if(Ext.getCmp('generalpanel').getActiveTab().getId() == 'detailGraphic'){
                Ext.MessageBox.alert('Attention!', 'Ceia ce ati facut nu are sens.');
        	} else {
                Ext.getCmp('userlist').getStore().load({
                    params: {storeTip: 'grid', acction: 'pro', period: 'year', pro: e.data.iddd, UserID: UserID}
                });
        	}
    	}
    },
    removeSelected: function(combo, record) {
        combo.setValue('');
    },
    loadDetails: function(grid, record) {
        periods = Ext.getCmp('detail').getValue();
        if(periods == 1) {
        	Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'cild', period: 'day', cild: record.data.cild, UserID: UserID}
            });
        }
        if(periods == 2) {
            Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'cild', period: 'week', cild: record.data.cild, UserID: UserID}
            });
        }
        if(periods == 3) {
            Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'cild', period: 'luni', luna: monthId, cild: record.data.cild, UserID: UserID}
            });
        }
        if(periods == 4) {
            Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'cild', period: 'year', cild: record.data.cild, UserID: UserID}
            });
        }
        if(periods == 5) {
            Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'cild', period: 'perioada', from: fromData, to: toData, cild: record.data.cild, UserID: UserID}
            });
        }
    },

    showaddNew: function(grid, record) {
    	Ext.getStore('StoreComboSubpro').load({params: {storeTip: 'comboSubPro', acction: 'combosubpro', UserID: UserID},
    	callback : function() {
    		var view = Ext.widget('addrow');
    		view.down('#update').hide();
    	}});
    },
    addNew: function(obj, record) {
    	var win = obj.up('window'),
        form = win.down('form'),
        values = form.getValues();
   	 	subpro = win.down('subpro').getRawValue();
   	 	cild = win.down('cild').getRawValue();
   	 	daa = win.down('#data').getValue();
   	    Ext.Ajax.request({
            url: 'data/tools.php',
            scope: this,
            method: "GET",
            params: {
                storeTip: 'addRow',
                acction: 'addrow',
                denum: values.denum,
                subpro: values.idsubpro,
                cild: values.idcild,
                data: Ext.Date.format(daa, 'Y-m-d'),
                suma: values.suma,
                UserID: UserID
                },
            success: function(response){
                var obj = Ext.decode(response.responseText);
                var responseData = obj.rows[0];
                var myrecord = Ext.create('AM.model.ModelGrid', {
                    id: responseData.it,
                    denum: values.denum,
                	subpro: subpro,
                	idsubpro: values.idsubpro,
                	cild: cild,
                	idcild: values.idcild,
                	data: daa,
                	suma: values.suma,
                	tip: responseData.tip
                });
                Ext.getCmp('userlist').getStore().insert(0, myrecord);
                Ext.getStore('StoreDetail').load({
                    params: {storeTip: 'detail', acction: 'dey', UserID: UserID}
                });
                Ext.getStore('StoreGrafic').load({
                	params: {storeTip: 'graphic', acction: 'grafic', UserID: UserID}
                });
                Ext.getStore('StoreDetailSum').load({
                    params: {storeTip: 'details_sum', acction: 'day', UserID: UserID}
                });
                win.close();
            }
        });
    },
    editUser: function(grid, record) {
    	Ext.getStore('StoreComboSubpro').load({params: {storeTip: 'comboSubPro', acction: 'combosubpro', UserID: UserID}});
    	Ext.getStore('StoreComboCild').load({params: {storeTip: 'comboChild', acction: 'combocild', UserID: UserID},
    	callback : function() {
    		var view = Ext.widget('addrow');
        	view.down('#save').hide();
        	view.down('form').getForm().loadRecord(record);	
    	}});
    },
    updateUser: function(button,s){
        var win = button.up('window'),
         form = win.down('form'),
         record = form.getRecord(),
         values = form.getValues();
         subpro = win.down('subpro').getRawValue();
         cild = win.down('cild').getRawValue();

         daa = win.down('#data').getValue();
         Ext.Ajax.request({
             url: 'data/tools.php',
             scope: this,
             method: "GET",
             params: {
                 storeTip: 'updateRow',
                 acction: 'updaterow',
                 denum: values.denum,
                 subpro: values.idsubpro,
                 cild: values.idcild,
                 data: Ext.Date.format(daa, 'Y-m-d'),
                 suma: values.suma,
                 id: record.data.id
                 },
             success: function(response){
                 var obj = Ext.decode(response.responseText);
                 var responseData = obj.rows[0];
                 record.set('denum', values.denum);
                 record.set('subpro', subpro);
                 record.set('idsubpro', values.idsubpro);
                 record.set('cild', cild);
                 record.set('idcild', values.idcild);
                 record.set('data', daa);
                 record.set('suma', values.suma);
                 record.set('tip', responseData.tip);
                 Ext.getStore('StoreDetail').load({
                    params: {storeTip: 'detail', acction: 'dey', UserID: UserID}
                 });
                 Ext.getStore('StoreGrafic').load({
                    params: {storeTip: 'graphic', acction: 'grafic', UserID: UserID}
                 });
                 Ext.getStore('StoreDetailSum').load({
                     params: {storeTip: 'details_sum', acction: 'day', UserID: UserID}
                 });
                 win.close();
             }
         });
    },
    loadStoreCild: function(obj,s){
    	var win = obj.up('window');
    	if(win.down('tip')){
    		win.down('tip').hide();
    	}
    	
	   	var StoreComboCild = this.getStoreComboCildStore();
	   	StoreComboCild.load({
			params: {storeTip: 'comboChildd', acction: 'combocildd', subpro: s[0].data.id, UserID: UserID}
		});
   	},
   	loadStoreDetail: function(obj,s){
   		if(s[0].data.id==1){
//   			console.log('Zilnic');
   			Ext.getCmp('luni').hide();
   			Ext.getCmp('from').hide();
   			Ext.getCmp('to').hide();
   			var StoreDetail = this.getStoreDetailStore();
		   	StoreDetail.load({
				params: {storeTip: 'detail', acction: 'dey', UserID: UserID}
			});
			var StoreDetailSum = this.getStoreDetailSumStore();
            StoreDetailSum.load({
                params: {storeTip: 'details_sum', acction: 'day', UserID: UserID}
            });
            Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'dateout', period: 'day', UserID: UserID}
            });
   		}else if(s[0].data.id==2){
//   			console.log('Saptamanal');
   			Ext.getCmp('luni').hide();
   			Ext.getCmp('from').hide();
   			Ext.getCmp('to').hide();
   			var StoreDetail = this.getStoreDetailStore();
		   	StoreDetail.load({
				params: {storeTip: 'detail', acction: 'week', UserID: UserID}
			});
			var StoreDetailSum = this.getStoreDetailSumStore();
            StoreDetailSum.load({
                params: {storeTip: 'details_sum', acction: 'week', UserID: UserID}
            });
            Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'dateout', period: 'week', UserID: UserID}
            });
   		}else if(s[0].data.id==3){
//   			console.log(new Date().getMonth()+1);//TODO
   			var monthId = new Date().getMonth()+1;
            var monthValue = Ext.getCmp('luni').getStore().getAt(monthId-1).get('task');
            Ext.getCmp('luni').setRawValue(monthValue);
   			Ext.getCmp('luni').show();
   			Ext.getCmp('from').hide();
   			Ext.getCmp('to').hide();
            var StoreDetail = this.getStoreDetailStore();
                console.log(StoreDetail);
                StoreDetail.load({
                    params: {storeTip: 'detail', acction : 'luni', luna: monthId, UserID: UserID}
                });
            var StoreDetailSum = this.getStoreDetailSumStore();
            StoreDetailSum.load({
                params: {storeTip: 'details_sum', acction: 'luni', luna: monthId, UserID: UserID}
            });
            Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'dateout', period: 'luni', luna: monthId, UserID: UserID}
            });

   		}else if(s[0].data.id==4){
//   			console.log('Anual');
   			Ext.getCmp('luni').hide();
   			Ext.getCmp('from').hide();
   			Ext.getCmp('to').hide();
   			var StoreDetail = this.getStoreDetailStore();
   			StoreDetail.load({
				params: {storeTip: 'detail', acction: 'year', UserID: UserID}
			});
			var StoreDetailSum = this.getStoreDetailSumStore();
            StoreDetailSum.load({
                params: {storeTip: 'details_sum', acction: 'year', UserID: UserID}
            });
            Ext.getCmp('userlist').getStore().load({
                            params: {storeTip: 'grid', acction: 'dateout', period: 'year', UserID: UserID}
            });
   		}else if(s[0].data.id==5){
//   			console.log('Pe perioada');
   			Ext.getCmp('luni').hide();
   			Ext.getCmp('from').show().setValue('');
   			Ext.getCmp('to').show().setValue('');
   		}
   	},
   	loadLuni: function(obj, s){
//	   	console.log('Select Luni');
	   	monthId = s[0].data.id;
	   	var StoreDetail = this.getStoreDetailStore();
			console.log(StoreDetail);
		   	StoreDetail.load({
				params: {storeTip: 'detail', acction : 'luni', luna: monthId, UserID: UserID}
			});
		var StoreDetailSum = this.getStoreDetailSumStore();
        StoreDetailSum.load({
            params: {storeTip: 'details_sum', acction: 'luni', luna: monthId, UserID: UserID}
        });
        Ext.getCmp('userlist').getStore().load({
            params: {storeTip: 'grid', acction: 'dateout', period: 'luni', luna: monthId, UserID: UserID}
        });
   	},
   	loadTo: function(obj, s){
	    var d = Ext.getCmp('from').getValue();
	    var b = Ext.getCmp('to').getValue();
	   	if(d & b){
	   		fromData = Ext.util.Format.date(d, 'Y-m-d');
		   	toData = Ext.util.Format.date(b, 'Y-m-d');
		   	var StoreDetail = this.getStoreDetailStore();
		   	StoreDetail.load({
		   		params: {storeTip: 'detail', acction: 'perioada', from: fromData, to: toData, UserID: UserID}
		   	});
		   	var StoreDetailSum = this.getStoreDetailSumStore();
            StoreDetailSum.load({
                params: {storeTip: 'details_sum', acction: 'perioada', from: fromData, to: toData, UserID: UserID}
            });
            Ext.getCmp('userlist').getStore().load({
                params: {storeTip: 'grid', acction: 'dateout', period: 'perioada', from: fromData, to: toData, UserID: UserID}
            });
	   	}
   	},
   	addTree: function(obj,s){
	   	var win = obj.up('window');
	   	pro = win.down('pro').getValue();
    	subpro = win.down('subpro').getValue();
    	cild = win.down('cild').getValue();
    	
    	proraw = win.down('pro').getRawValue();
    	subproraw = win.down('subpro').getRawValue();
    	cildraw = win.down('cild').getRawValue();
    	if((parseInt(pro)==pro)!=true){
//    		console.log('Insert new pro');
    		tip = win.down('tip').getValue();
    		Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
    			params: {
    			    storeTip: 'addPro',
    				acction: 'addpro',
    				pro: pro,
    				subpro: subproraw,
    				cild: cildraw,
    				tip: tip,
    				UserID: UserID
    			},
    			success : function() {
    				win.close();
    				Ext.getStore('StoreTree').load({params: { acction :'tree',UserID:UserID}});
    			}
    		});
    	}else if((parseInt(subpro)==subpro)!=true){
            tip = win.down('tip').getValue();
            Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
                params: {
                    storeTip: 'addSubPro',
                    acction: 'addsubpro',
                    pro: pro,
                    subpro: subproraw,
                    cild: cildraw,
                    tip: tip,
                    UserID: UserID
                },
                success: function(response){
                    win.close();
                    Ext.getStore('StoreTree').load({params: {acction: 'tree', UserID: UserID}});
                }
            });
    	}else if((parseInt(cild)==cild)!=true){
            Ext.Ajax.request({
                url: 'data/tools.php',
                scope: this,
                method: "GET",
                params: {
                    storeTip: 'addChild',
                    acction : 'addcild',
                    pro:pro,
                    subpro:subpro,
                    cild:cildraw,
                    UserID:UserID
                    },

                success : function() {
                    win.close();
                    Ext.getStore('StoreTree').load({params: { acction: 'tree', UserID: UserID}});
                }
            });
        }
   	},
   	removeChart : function() {
        var chart = Ext.getCmp('chartDetail');
        var series = chart.series.items,
            surface = chart.surface,
            length = series.length,
            len = surface.groups.keys.length,
            array = [],
            i = 0,
            j = 0,
            t = 0;

        for(; i < length; i++) {
            array = Ext.Array.merge(array, series[i].group.keys);
        }
        chart.series.clear();
        for(; j < array.length; j++) {
            surface.items.getByKey(array[j]).destroy();
        }
        for(; t < len; t++) {
            surface.groups.items[t].destroy();
        }
    },
    addSeries : function(title, field) {
        var chart = Ext.getCmp('chartDetail');
        chart.series.add({
            type: 'line',
            axis: 'left',
            smooth: true,
            title: title,
            xField: 'day',
            yField: field,
            highlight: {
                size: 7,
                radius: 7
            },
            markerConfig: {
                type: 'cross',
                size: 4,
                radius: 4,
                'stroke-width': 0
            },
            tips: {
                trackMouse: true,
                width: 160,
                height: 20,
                renderer: function(storeItem, item) {
                    this.setTitle(storeItem.get(field) + ' în ' + storeItem.get('day').substr(0, 3));
                }
            }
        });
    }
});